let app = getApp();
import {util} from '../../utils/util';

Page({
    data: {
        top_Boolean: false,
        not_login_Boolean: false,
        student_Boolean: false,
        student_notLogin_Boolean: true,
        //应用的token
        Token: '',
        //用户在钉钉的ID
        DingUserId: '',
        //学校地址
        SchoolAddres: '',
        //角色
        Role: '',
        //中台id
        EId: '',
        authCode: '',
        //无水印电话
        mobile: '',
        //有水印电话
        mobilepass: '',
        //用户名称
        username: '',
        corpId: ''
    },

    onLoad(query) {
        let _this = this;
        //app.token==''时没有token所以还没登录需要前往登录，有登录token的话就可以直接渲染写实页面
        var token = app.token;
        if (token == '') {
            console.log('未登录，前往卡包或者注册登录')
            _this.getSystemInfo();
        } else {
            console.log('已登录直接根据角色渲染')
            // 角色   1是学生，2是老师，3是公众
            var Role = app.Role
            if (Role == 1) {
                console.info('学生:' + _this.Role)
                _this.setData({
                    top_Boolean: false,
                    student_Boolean: true,
                    not_login_Boolean: false,
                    student_notLogin_Boolean: false,
                })
            } else if (Role == 2) {
                console.info('老师身份' + _this.Role)
                _this.setData({
                    top_Boolean: true,
                    student_Boolean: false,
                    not_login_Boolean: false,
                    student_notLogin_Boolean: false,
                })
            } else {
                _this.setData({
                    top_Boolean: false,
                    student_Boolean: false,
                    not_login_Boolean: true,
                    student_notLogin_Boolean: false,
                })
            }
        }
    },
    getTeacherWebIndex() {
        const head = this.selectComponent('#head')
        const student = this.selectComponent('#student')

        if (this.data.student_Boolean) {
            student.getStudentIndex();
        } else if (this.data.top_Boolean) {
            head.refreshData();
        } else {
            wx.stopPullDownRefresh()
        }

    },


    onPullDownRefresh() {
        // 页面被下拉
        this.getTeacherWebIndex()
    },


    //获取当前小程序的运行环境是企业微信还是微信
    getSystemInfo() {
        let _this = this;
        var isWxWork = false;
        wx.getSystemInfo({
            success(res) {
                console.log(res.environment);
                isWxWork = res.environment == 'wxwork';
                if (!isWxWork) {
                    // 当前环境不是企业微信
                    _this.wxLogin()
                } else {
                    // 当前环境是企业微信
                    _this.qyLogin()
                }
            }
        })
    },

    //企业微信获取code
    qyLogin() {
        let _this = this
        wx.qy.login({
            success: function (res) {
                if (res.code) {
                    console.log('企业微信authCode:' + res.code)
                    _this.setData({
                        authCode: res.code
                    })
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        });
    },

    //微信获取code
    wxLogin() {
        let _this = this
        wx.login({
            success: function (res) {
                console.log('微信authCode:' + res.code);
                if (res.code) {
                    util.showLoading();
                    wx.request({
                        url: app.LkServerAddress + '/dingtalk/wxLogin',
                        method: 'GET',
                        data: {
                            authCode: res.code,
                            userType: 3,
                        },
                        dataType: 'json',
                        success: function (res) {
                            console.log('success----', res);
                            util.showError("微信登录成功");
                            //(res.data.code==0)返回的code等于时证明接口调用返回正常
                            if (res.data.code == 0) {
                                app.cardToken = res.data.info.cardToken;
                                //判断本平台是否有卡
                                if (res.data.info.hasThisPlatformCard) {
                                    //有卡进来再判断有多张卡还是只有一张
                                    if (res.data.info.userCard == null) {
                                        //多张卡，进卡包
                                        wx.navigateTo({
                                            url: '/pages/admin/student-toggle-login/index'
                                        })
                                    } else {
                                        var secret = res.data.info.userCard.secret;
                                        app.username = res.data.info.userCard.name;
                                        //调用钉钉端登录接口
                                        util.loginByDingTalk(secret).then(function (res) {
                                            console.log('success----', res)
                                            if (res.data.code == 0) {
                                                app.token = res.data.info.token;
                                                util.getUserInfo().then(function (res) {
                                                    if (res.data.code == 0) {
                                                        if (app.Role == 1) {
                                                            console.info('学生和公众身份:' + _this.Role)
                                                            _this.setData({
                                                                top_Boolean: false,
                                                                student_Boolean: true,
                                                                not_login_Boolean: false,
                                                                student_notLogin_Boolean: false,
                                                            })
                                                        } else if (app.Role == 2) {
                                                            console.info('老师身份' + _this.Role)
                                                            _this.setData({
                                                                top_Boolean: true,
                                                                student_Boolean: false,
                                                                not_login_Boolean: false,
                                                                student_notLogin_Boolean: false,
                                                            })
                                                        }
                                                    } else {
                                                        util.showError(res.data.message);
                                                    }
                                                })
                                            } else {
                                                util.showError(JSON.stringify(res.data.message));
                                            }
                                        })
                                    }
                                } else {
                                    //没有卡
                                    util.showError('没有卡');
                                    _this.setData({
                                        top_Boolean: false,
                                        student_Boolean: false,
                                        not_login_Boolean: false,
                                        student_notLogin_Boolean: true,
                                    })
                                }
                            } else {
                                console.log(res.data.code)
                                util.showError(JSON.stringify(res.data.message))
                            }
                        },
                        fail: function (res) {
                            console.log("httpRequestFail---", res)
                            util.showError('登录失败：' + JSON.stringify(res.errMsg))
                        },
                        complete: function (res) {
                            util.hideLoading();
                        }
                    });
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            },
            fail: function (res) {
                util.showError("调用登录方法失败" + res)
                _this.setData({
                    top_Boolean: false,
                    student_Boolean: false,
                    not_login_Boolean: false,
                    student_notLogin_Boolean: true,
                })
            }
        });
    },


    onReady() {
        // 页面加载完成
    },
    onShow() {
        // 页面显示
    },
    onHide() {
        // 页面隐藏
    },
    onUnload() {
        // 页面被关闭
    },
    onTitleClick() {
        // 标题被点击
    },

    onReachBottom() {
        // 页面被拉到底部
    },
    onShareAppMessage() {
        // 返回自定义分享信息
        return {
            title: 'My App',
            desc: 'My App description',
            path: 'pages/index/index',
        };
    },
});